package hut.ai.team.screen;

import hut.ai.team.core.DragDropStage;

import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;

public class MenuScreen extends AbstractScreen {
	private MyStage stage;
	private TextureRegionDrawable OnePlayer, OnePlayerPressed, TwoPlayers,
			TwoPlayersPressed, Option, OptionPressed, Exit, ExitPressed;
	public static int BACK = 1;
	public static int GameMode;
	
	public MenuScreen() {
		stage = new MyStage();
//		TextureAtlas bgAtlas = game.manager.get("images/bg/backgrounds.atlas");
		TextureAtlas uiAtlas = game.manager.get("images/ui/ui.atlas");

		Image bg = new Image(uiAtlas.findRegion("bg1"));
		bg.setFillParent(true);
		stage.addActor(bg);
		
		OnePlayer = new TextureRegionDrawable(uiAtlas.findRegion("oneplayer"));
		OnePlayerPressed = new TextureRegionDrawable(uiAtlas.findRegion("oneplayer_click"));
		TwoPlayers = new TextureRegionDrawable(uiAtlas.findRegion("twoplayer"));
		TwoPlayersPressed = new TextureRegionDrawable(uiAtlas.findRegion("twoplayer_click"));
		Option = new TextureRegionDrawable(uiAtlas.findRegion("option"));
		OptionPressed = new TextureRegionDrawable(uiAtlas.findRegion("option_click"));
		Exit = new TextureRegionDrawable(uiAtlas.findRegion("exit_main"));
		ExitPressed = new TextureRegionDrawable(uiAtlas.findRegion("exit_main_click"));

		Button btnOnePlayer = new Button(OnePlayer, OnePlayerPressed);
		btnOnePlayer.setPosition(game.convertWidth(72), game.convertHeight(1200));
//		btnOnePlayer.setName("One Player");
		btnOnePlayer.addAction(Actions.sequence(Actions.delay(2f), Actions.moveTo(game.convertWidth(72), game.convertHeight(453), 1.5f)));
		btnOnePlayer.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				// TODO Auto-generated method stub
				GameMode = 1;
				game.setScreen(MainScreen.getInstance());
			}
		});
		stage.addActor(btnOnePlayer);
		
		Button btnTwoPlayers = new Button(TwoPlayers, TwoPlayersPressed);
		btnTwoPlayers.setPosition(game.convertWidth(66), game.convertHeight(1150));
		btnTwoPlayers.addAction(Actions.sequence(Actions.delay(1.5f), Actions.moveTo(game.convertWidth(66), game.convertHeight(370), 1.5f)));
//		btnTwoPlayers.setName("Two players");
		btnTwoPlayers.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				// TODO Auto-generated method stub
				GameMode = 2;
				game.setScreen(MainScreen.getInstance());
			}
		});
		stage.addActor(btnTwoPlayers);
		
		Button btnOption = new Button(Option, OptionPressed);
		btnOption.setPosition(game.convertWidth(141), game.convertHeight(1100));
		btnOption.addAction(Actions.sequence(Actions.delay(1f), Actions.moveTo(game.convertWidth(141), game.convertHeight(291), 1.5f)));
//		btnOption.setName("Option");
		btnOption.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				// TODO Auto-generated method stub
				game.setScreen(SettingScreen.getInstance());
			}
		});
		stage.addActor(btnOption);
		
		Button btnExit = new Button(Exit, ExitPressed);
		btnExit.setPosition(game.convertWidth(164), game.convertHeight(1050));
		btnExit.addAction(Actions.sequence(Actions.delay(0.5f), Actions.moveTo(game.convertWidth(164), game.convertHeight(214), 1.5f)));
		btnExit.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				System.exit(0);
			}
		});
		stage.addActor(btnExit);
	}

	@Override
	public void show() {
		super.show();
		game.sound.nhacnen.stop();
		BACK = 1;
	}

	@Override
	public Stage getStage() {
		return stage;
	}
	
	@Override
	protected void onBackKeyPress() {
		System.exit(0);
	}
	
	public static MenuScreen instance = null;

	public static Screen getInstance() {
		if (instance == null) {
			instance = new MenuScreen();
		}
		return instance;
	}

	class MyStage extends DragDropStage {
		public MyStage() {
			super(game.assumeWidth, game.assumeHeight, false);
		}
	}
}
